package com.hualian.coupon.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualian.coupon.pojo.Coupon;
import com.hualian.coupon.service.CouponService;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lxd
 * @Description TODO
 * @date 2020/10/22 11:14
 * @Version 1.0
 */

@RestController
@RequestMapping("/cop")
public class CouponController {

    @Resource
    CouponService couponService;

    @PostMapping("/add")
    public int add(@RequestBody @Valid Coupon coupon) {
        return couponService.add(coupon);
    }


    @PostMapping("/list")
    public Map<String,Object> getCopList(@RequestParam Integer page) {
        PageHelper.startPage(page, 10);
        List<Coupon> list = couponService.list();
        PageInfo<Coupon> pageInfo = new PageInfo<>(list);
        Map<String,Object> map = new HashMap<>(4);
        map.put("data", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return map;
    }

    @RequestMapping("/exCouponList")
    public void handle(HttpServletResponse response) throws IOException {
        //获取优惠券信息
        List<Coupon> coupons = couponService.list();

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("优惠券");
        String fileName = "优惠券.xls";

        //实现中文文件名转换
        fileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");
        //headers表示Excel表中第一行的表头
        String[] headers = {"id","name","getIndate","useIndate","desc","detail"};
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //处理时间格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //添加数据行
        int rowNum = 1;
        for (Coupon item : coupons
        ) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);
            dataRow.createCell(1).setCellValue(item.getName());
            dataRow.createCell(2).setCellValue(dateFormat.format(item.getGetIndate()));
            dataRow.createCell(3).setCellValue(dateFormat.format(item.getUseIndate()));
            dataRow.createCell(4).setCellValue(item.getDesc());
            dataRow.createCell(5).setCellValue(item.getRole());
            rowNum++;
        }

        //返回给前端下载
        response.setContentType("application/octet-stream");
        //以附件形式访问
        response.setHeader("Content-disposition","attachment;filename="+fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    @PostMapping("/myCoupon")
    public List<Map<String,Object>> myCoupon(@RequestParam String phone) {
        return couponService.queryCop(phone);
    }
}
